<template>
    <div class="contract-detail-page contract-detial-page">
        <Card>
            <Row style="text-align: right;">
                <div>
                    <div class="contract-detial-btns top-btn" v-if="isApprove">
                        <Button type="primary" @click="back">返回</Button>
                        <Button type="primary" @click="approve('pass')" v-if="auditOp!='view'">通过</Button>
                        <Button type="primary" @click="approve('reject')" v-if="auditOp!='view'">拒绝</Button>
                    </div>
                    <div class="contract-detial-btns" v-else>
                        <Button type="primary" @click="back">返回</Button>
                        <!--                        <Button type="primary" @click="save" v-if="op!='view'">保存</Button>-->
                        <Button type="primary" @click="findByContractNo" v-if="op!='view'">提交</Button>
                    </div>
                </div>
            </Row>
            <Collapse v-model="openPanel" class="contract-detail-collapse">
                <Panel name="1" v-if="isApprove||this.Obj.processStatus==='approved'">
                    日志
                    <div class="content" slot="content">
                        <approveHistory :Obj="Obj"/>
                    </div>
                </Panel>
                <Panel name="2">
                    基本信息
                    <div class="content" slot="content">
                        <BaseInfo :formData="pageInfo"/>
                    </div>
                </Panel>
                <Panel name="3">
                    合同信息
                    <div class="content" slot="content">
                        <contractInfo ref="contractInfo" :Obj="Obj"/>
                    </div>
                </Panel>
                <Panel name="4">
                    金融产品
                    <div class="content" slot="content">
                        <FinanceInfo :Obj="Obj"/>
                    </div>
                </Panel>
                <Panel name="5">
                    试算信息
                    <div class="content" slot="content">
                        <trailCalculate
                            ref="trailCalculate"
                            :op="op"
                            :isSP="isSP"
                            :isTrail="isTrail"
                            :applyNo="Obj.applyNo"
                            :contractNo="Obj.contractNo"
                            :isSixPeriod="isSixPeriod"
                            :quotationId="quotationId"
                            :contractForm="contractForm"
                            :isApprove="isApprove"
                            :auditOp="auditOp"
                            :Obj="Obj"
                            @backInfo="trailCalculatebackInfo"
                        />
                    </div>
                </Panel>
                <Panel name="6" v-if="(auditOp!=='view'&&isApprove)||this.Obj.processStatus==='approved'">
                    当前用户: {{userInfo ? userInfo.userRealName :''}}
                    <div class="content" slot="content">
                        <ContractCurrentUser ref="contractCurrentUser" :Obj="Obj" @childUserForm="childUserForm"/>
                    </div>
                </Panel>
            </Collapse>
        </Card>
    </div>
</template>

<script>
    import {mapGetters} from "vuex";
    // 日志
    import approveHistory from "_p/basic/pages/contract/common/approve-history/approve-history";
    // 基本信息
    import BaseInfo from "_p/basic/pages/contract/common/base-info/index";
    // 合同信息
    import contractInfo from "_p/basic/pages/contract/common/contract-info/contract-info";
    // 金融产品
    import FinanceInfo from "_p/basic/pages/contract/common/finance-info/finance-info";
    //试算信息
    import trailCalculate from "_p/basic/pages/contract/common/trial-info/trail-calculate.vue";
    // 当前用户
    import ContractCurrentUser from "_p/basic/pages/contract/common/current-user/contractCurrentUser";
    import {add, sub} from "_p/basic/assets/contract/js/utils";

    import {
        saveManagePrepaymentApply,
        getStaySettleDetailByApplyNo,
        trailLaunchProcess,
        trailSubmitProcess,
    } from "@/projects/basic/api/contract/clearance/stay-settle";
    import {findByContractNo} from "_p/basic/api/contract/activated-case";


    export default {
        components: {
            approveHistory,
            BaseInfo,
            contractInfo,
            FinanceInfo,
            trailCalculate,
            ContractCurrentUser
        },
        name: "applySettle",
        data() {
            return {
                //基本数据【caseNo, contractNo, roleType】
                Obj: this.afs.getPageParams(this),
                openPanel: [0, 2, 3, 4, 5, 6],
                //是否SP
                isSP: this.afs.getPageParams(this).isSP,
                //状态【add,edit,view】
                op: this.afs.getPageParams(this).op,
                //审核状态【add,edit,view】
                auditOp: this.afs.getPageParams(this).auditOp,
                //是否是审批页面
                isApprove: this.afs.getPageParams(this).isApprove,
                //提前还款申请ID
                applyId: this.afs.getPageParams(this).applyId,
                //提前还款试算ID
                quotationId: this.afs.getPageParams(this).quotationId,
                //是否试算
                isTrail: false,
                //试算结果信息
                trailForm: {},
                //试算清单信息
                contractForm: {},
                //是否满六期
                isSixPeriod: false,
                //方法执行判断，默认否
                submitLoading: false,
                //基本信息
                pageInfo: {
                    applyDate: this.afs.getPageParams(this).applyDate,
                    endDate: this.afs.getPageParams(this).endDate,
                    applyBy: this.afs.getPageParams(this).applyBy,
                    caseNo: this.afs.getPageParams(this).caseNo,
                    createBy: this.afs.getPageParams(this).createBy
                },
            }
        },
        computed: {
            ...mapGetters({userInfo: "userInfo", roleType: "roles"})
        },
        methods: {
            back() {
                this.afs.closeTab(this);
                this.$router.push({path: '/apply'});
            },
            // 审核等按钮
            approve(name) {
                if (!name) return;
                let t, val;
                switch (name) {
                    case "pass":
                        t = "审核通过";
                        val = "suggestCheckF";
                        break;
                    case "reject":
                        t = "拒绝";
                        val = "suggestRejectF";
                        break;
                }
                this.trailForm.applyId = this.applyId;
                this.trailForm.quotationId = this.quotationId;
                this.trailForm.approvalOperating = val;
                if (name === 'reject') {
                    this.trailForm.otherCost = sub(this.$refs.trailCalculate.resultForm.otherCost, this.$refs.trailCalculate.resultForm.prepaymentCharge);
                }
                // 验证 contractCurrentUser 组件数据 传给下个方法
                this.$refs.contractCurrentUser.formValidate("childUserForm", this.trailForm, name);
            },
            // contractCurrentUser 组件数据
            childUserForm(data) {
                if (!data) return;
                let t;
                switch (data.approvalOperating) {
                    case "suggestCheckF":
                        t = "通过";
                        break;
                    case "sendBack":
                        t = "退件";
                        break;
                    case "suggestRejectF":
                        t = "拒绝";
                        break;
                }
                this.$Modal.confirm({
                    title: `确认${t}？`,
                    onOk: () => {
                        this.trailSubmitProcess(data)
                    }
                });
            },
            trailSubmitProcess(data) {
                this.submitLoading = true;
                trailSubmitProcess(data).then(res => {
                    this.submitLoading = false;
                    if (res.code === "0000") {
                        this.$Message.success("操作成功");
                        this.back();//退回
                    }
                }).catch(() => {
                    this.submitLoading = false;
                });
            },
            save() {
                // console.info("保存的信息："+JSON.stringify(this.trailForm));
                if (!("add" == this.op && !this.trailForm.isCal)) {
                    this.trailForm.applyId = this.applyId;
                    this.trailForm.quotationId = this.quotationId;
                    this.trailForm.custName = this.Obj.custName;
                    this.trailForm.businessSource = this.Obj.businessSource;
                    this.trailForm.phone = this.$refs.contractInfo.formData.basicCustBaseInfo.phone;
                    saveManagePrepaymentApply(this.trailForm).then(res => {
                        if (res.code === "0000") {
                            this.op = "edit";
                            this.quotationId = res.data.quotationId;
                            this.applyId = res.data.id;
                            this.$Message.success("保存成功");
                            //更新基本信息
                            // console.info("保存完的数据："+JSON.stringify(res.data))
                            this.pageInfo.applyDate = res.data.prepaymentRequestTime;
                            this.pageInfo.applyBy = res.data.createBy;
                        }
                    });
                } else {
                    this.$Message.warning("请先计算！");
                }
            },
            findByContractNo() {
                let params = {
                    contractBusiness: ["advanceSettle"],
                    contractNo: this.Obj.contractNo,
                    handleBusiness: "advanceSettle",
                };
                findByContractNo(params).then(res => {
                    if (res.code === "0000") {
                        if (res.msg === "success") {
                            this.submit();
                        }
                    }
                });
            },
            submit() {
                this.$Modal.confirm({
                    title: "确认提交？",
                    onOk: () => {
                        if (!("add" == this.op && !this.trailForm.isCal)) {
                            this.trailForm.applyId = this.applyId;
                            this.trailForm.quotationId = this.quotationId;
                            this.trailForm.custName = this.Obj.custName;
                            this.trailForm.businessSource = this.Obj.businessSource;
                            this.trailForm.phone = this.$refs.contractInfo.formData.basicCustBaseInfo.phone;
                            this.trailForm.otherCost = add(this.$refs.trailCalculate.resultForm.otherCost, this.$refs.trailCalculate.resultForm.prepaymentCharge);
                            //保存的方法
                            this.submitLoading = true;
                            saveManagePrepaymentApply(this.trailForm).then(res => {
                                if (res.code === "0000") {
                                    this.op = "edit";
                                    this.quotationId = res.data.quotationId;
                                    this.applyId = res.data.id;
                                    //正式提交
                                    this.trailForm.applyId = this.applyId;
                                    this.trailForm.quotationId = this.quotationId;
                                    // console.info("提交的日志："+JSON.stringify(this.trailForm));
                                    trailLaunchProcess(this.trailForm).then(res => {
                                        this.submitLoading = false;
                                        if (res.code === "0000") {
                                            this.$Message.success("操作成功");
                                            this.back();//退回
                                        }
                                    }).catch(() => {
                                        this.submitLoading = false;
                                    });
                                }
                            }).catch(() => {
                                this.submitLoading = false;
                            });
                            ;
                        } else {
                            this.$Message.warning("请先计算！");
                        }
                    }
                });
            },
            init() {
                getStaySettleDetailByApplyNo({applyNo: this.Obj.applyNo}).then(res => {
                    if (res.code === "0000") {
                        this.contractForm = res.data;
                        // console.info("查询合同信息："+JSON.stringify(this.contractForm))
                        if (this.contractForm.alreadyPeriods < 6) {
                            this.isSixPeriod = true;
                        }
                    }
                });

            },
            //试算结果
            trailCalculatebackInfo(param) {
                this.trailForm = param;
            },
        },
        mounted() {
            this.init();
        },
        created() {
        },
    }
</script>
<style lang="less">
    @import "../../../styles/common/form.less";
</style>
